package com.android.love.db;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.webkit.WebChromeClient.CustomViewCallback;

import com.android.love.util.Constant;
import com.android.love.util.LogUtil;
import com.android.love.vo.Goods;

/**
 * 数据库工具类(封装对数据库的增删改查操作)
 * 
 * @author adison
 * 
 * 
 */
public class DatabaseUtil {
	private static final String TAG = "DatabaseUtil";
	private static DatabaseUtil instance;
	/** 数据库帮助类 **/
	private DBHelper dbHelper;

	public synchronized static DatabaseUtil getInstance(Context context) {
		if (instance == null) {
			instance = new DatabaseUtil(context);
		}
		return instance;
	}

	/**
	 * 初始化
	 * 
	 * @param context
	 */
	private DatabaseUtil(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * 销毁
	 */
	public static void destory() {
		if (instance != null) {
			instance.onDestory();
		}
	}

	/**
	 * 销毁
	 */
	public void onDestory() {
		instance = null;
		if (dbHelper != null) {
			dbHelper.close();
			dbHelper = null;
		}
	}

	/**
	 * 添加或者删除收藏
	 * 
	 * @param context
	 * @param good
	 */
	public boolean operateFrovite(Goods good, boolean fav, String size,
			String color, String format) {
		// ContentResolver resolver = context.getContentResolver();
		long i = -1;
		ContentValues cv = new ContentValues();
		if (fav) {
			// 添加一条购物项
			cv.put(CartColumns.GOODS_ID, good.getGoods_id());
			cv.put(CartColumns.GOODS_IMAGE, good.getGoods_image());
			cv.put(CartColumns.GOODS_NAME, good.getGoods_name());
			cv.put(CartColumns.PRICE, good.getPrice());
			cv.put(CartColumns.OLD_PRICE, good.getOld_price());
			cv.put(CartColumns.COLOR, color);
			cv.put(CartColumns.SIZE, size);
			cv.put(CartColumns.FORMAT, format);

			String where = CartColumns.GOODS_ID + " = " + good.getGoods_id();
			i = dbHelper.update(CartColumns.TABLE_NAME, cv, where, null);
			if (i == 0) {
				i = dbHelper.insert(CartColumns.TABLE_NAME, null, cv);
			}
		} else {
			// 删除一条收藏
			String where = CartColumns.GOODS_ID + " = " + good.getGoods_id();
			i = dbHelper.delete(CartColumns.TABLE_NAME, where, null);

		}
		return i != -1;

	}

	/**
	 * 批量删除收藏
	 * 
	 * @param context
	 * @param content
	 */
	public int deleteFrovite(List<Goods> lists) {
		ContentValues cv = new ContentValues();
		int row = 0;
		for (Goods good : lists) {
			// 删除一条收藏
			String where = CartColumns.GOODS_ID + " = " + good.getGoods_id();
			row = dbHelper.delete(CartColumns.TABLE_NAME, where, null);
		}
		return row;

	}

	/**
	 * 查询购物车列表
	 * 
	 * @param context
	 * @return
	 */
	public List<Goods> queryCart() {
		List<Goods> contents = Collections.emptyList();
		// ContentResolver resolver = context.getContentResolver();
		Cursor cursor = dbHelper.query(CartColumns.TABLE_NAME, null, null,
				null, null, null, null);
		LogUtil.i(TAG, cursor.getCount() + "");
		if (cursor == null) {
			return contents;
		}
		contents = new ArrayList<Goods>();
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			Goods content = new Goods();
			List<String> colors = new ArrayList<String>();
			List<String> sizes = new ArrayList<String>();
			List<String> formats = new ArrayList<String>();
			content.setGoods_id(cursor.getLong(1));
			content.setGoods_image(cursor.getString(2));
			content.setGoods_name(cursor.getString(3));
			content.setPrice(cursor.getFloat(4));
			content.setOld_price(cursor.getFloat(5));
			colors.add(cursor.getString(6));
			content.setColor(colors);
			sizes.add(cursor.getString(7));
			content.setSize(sizes);
			formats.add(cursor.getString(8));
			content.setFormat(formats);
			content.setCount(1);
			contents.add(content);
		}
		if (cursor != null) {
			cursor.close();
		}
		// if (contents.size() > 0) {
		// return contents;
		// }
		return contents;
	}

	/**
	 * 是否已存在
	 * 
	 * @param goods_id
	 * @return
	 */
	public boolean isExitCart(long goods_id) {
		String where = CartColumns.GOODS_ID + " = " + goods_id;
		Cursor cursor = dbHelper.query(CartColumns.TABLE_NAME, null, where,
				null, null, null, null);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.close();
				return true;
			}
		}

		return false;

	}

}
